<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/9 0009
 * Time: 15:54
 */
namespace app\api\model;
use app\api\model\UserExtend;
class User extends \app\api\model\common
{
    protected $table = 'fd_user';

    public function getAlls($where){
        $res = User::where($where)->select();
        return $res = $res?collection($res)->toArray():'';
    }

    public function getOne($field,$where){
        $res = User::where($where)->field($field)->find();
        return $res = $res?$res->toArray():'';
    }


    /*
     * 根据传入的openid获取user表中的id
     * **/

    public function get_uid($openid){
        if(!$openid){
            return false;
        }
        $where='openid = '.$openid;
        $result=User::where($where)->field('id')->find();
        if($result){
            return $result['id'];
        }else{
            return false;
        }
    }

    /*
     * 绑定手机号验证
     *
     */
    public function phoneVer($phone,$openid){
        $swhere='phone = '.$phone;
        $where='openid = '.$openid;
        $field='phone';
        $sfield='ue_id';
        $data=UserExtend::getOne($field,$where);
        $sdata=UserExtend::getOne($sfield,$swhere);
        if($data && $data['phone']){
            return array('error'=>'100','errmsg'=>'您已绑定手机号');
        }elseif($sdata){
            return array('error'=>'101','errmsg'=>'此手机号已经被绑定，请您更换手机号');
        }else{
            return false;
        }
    }

    /*
     * 将绑定的手机号入库
     * **/
    public function bn_phone($openid,$phone){
        if(!$phone || !$openid){
            return false;
        }
        $data=['phone'=>$phone];
        $ud=array();
        $re=UserExtend::where(['openid'=>$openid])->field('nickname')->find();
        $ud['name']=$re['nickname'];
        $ud['phone']=$phone;
        $ud['created_at']=date('Y-m-d H:i:s', time());
        $user_id=User::insertGetId($ud);
        $result=UserExtend::where(['openid'=>$openid])->update($data);
        session($openid,$user_id);
        if($result && $user_id){
            return $user_id;
        }else{
            return false;
        }
    }

    /**
     * @desc  手机登录
     * @param $mmr_phone
     * @param $mmr_password
     * @return array
     */
    public function mobileLogin($mmr_phone,$mmr_password){
        if(!$mmr_phone || !$mmr_password){
            return self::noEnoughParams();
        }
        $res = D('Wechat/Member')->isPhoneMember($mmr_phone,$mmr_password);

        if(!empty($res)){
            $status = self::ERRCODE_LOGINSUSS;
        }else{
            $res    = '';
            $status = self::ERRCODE_NOREGEST;
            self::saveLog(json_encode($res).'--'.M()->getLastSql());
        }
        systemLog($res,'mobileLogin---'.$mmr_phone);

        return self::getResultData($status,$res);
    }

    public function mobileReg($mmr_phone,$mmr_password,$mmr_truename,$verify_code){
        if(!$mmr_phone || !$mmr_password || !$mmr_truename || !$verify_code){
            return self::noEnoughParams();
        }
        $msg = '';
        if(strlen($mmr_phone) != '11'){
            return self::getResultData(self::ERRCODE_ALL,'','请输入有效联系方式~');
        }
        if(strlen($mmr_password)<6|| strlen($mmr_password)>20){
            return self::getResultData(self::ERRCODE_ALL,'','密码长度6~20~');
        }

        //获取验证码
        $code_arr = D('Wechat/MessageVerify')->getMsgRow($mmr_phone);


        if($verify_code != $code_arr['mmv_code'] || time() > $code_arr['mmv_exp_time'] || $code_arr['mmv_status'] ==2 ){

            return self::getResultData(self::ERRCODE_ALL,'','验证码异常~');
        }


        //验证是否存在
        $res = D('Wechat/Member')->isPhoneMember($mmr_phone);
        if($res){
            return self::getResultData(self::ERRCODE_ALL,'','信息已存在，请勿重复注册~！');
        }
        $tmp_unionid = date('YmdHis').rand(10000,99999);
        $userInfo = array(
            'mmr_phone' => $mmr_phone,
            'mmr_password' => md5($mmr_password),
            'mmr_truename' => $mmr_truename,
            'mmr_ctime'    => time(),
            'unionid'      => $tmp_unionid,
            'tmp_unionid'  => $tmp_unionid,
            'mmr_source'   => getAgentNum()
        );

        $userInfo['member_ext']['mme_app_bind'] = 0;

        $res = D('Wechat/Member')->doMemberIns($userInfo);
        if($res){
            D('Wechat/MessageVerify')->saveMsgRow($mmr_phone,$verify_code);
            return self::getResultData(self::ERRCODE_SUCCESS);
        }else{
            return self::getResultData(self::ERRCODE_ALL);
        }
    }

    /**
     * @desc  获取短信验证码
     * @param $phone
     * @param string $msg
     * @return array
     */
    public function getMsgCode($phone,$msg=''){
        if(!$phone || !$msg){
            return self::noEnoughParams();
        }
        $msgCode = rand(234567,987654);

        //log_add('随机验证码'.$msgCode);

        $msg = str_replace('__CON__',$msgCode,$msg);

        //log_add("生成验证码：".$msg);
        $res = $this->send_msg($phone,$msg);
        //$res = send_msg($phone,$msg);
        /*$arr['mmv_code'] = $msgCode;
        $arr['mmv_ctime'] = time();
        $arr['mmv_exp_time'] = time()+3600;
        $arr['mmv_type'] = 1;
        $arr['mmr_phone'] = $phone;


        $query = D('Wechat/MessageVerify')->addVerifyRow($arr);

        if($query){
            $res = send_msg($phone,$msg);
        }*/

        return $res;


    }
}